package com.dpp.service.impl;

import com.dpp.context.BaseContext;
import com.dpp.entity.*;
import com.dpp.mapper.*;
import com.dpp.result.Result;
import com.dpp.service.ActivityRecordService;
import com.dpp.service.ProductInfoService;
import com.dpp.service.UserMoneyService;
import com.dpp.service.UserService;
import com.dpp.vo.Advert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class UserMoneyServiceImpl implements UserMoneyService {
    @Autowired
    private UserMoneyMapper userMoneyMapper;
    @Autowired
    private TakeOrderMapper takeOrderMapper;
    @Autowired
    private TransactionsMapper transactionsMapper;
    @Autowired
    private ActivityRecordServiceImpl activityRecordService;
    @Autowired
    private CouponBookMapper couponBookMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ProductInfoService productInfoService;
    @Autowired
    private ActivityRecordMapper activityRecordMapper;
    public void save(Orders orders) {
        String openid = orders.getOpenid();
        Integer productId = orders.getProductId();
        ProductInfo productInfo = productInfoService.selectByID(productId);
        String amountNew = String.valueOf(((int)(orders.getAmount()*100)));
        if(orders.getProductId().equals(1)){
            if(amountNew.equals(productInfo.getNewUserPrice()+"")){
                LocalDateTime now = LocalDateTime.now();
                ActivityRecord activityRecord = activityRecordService.selectByActivityId(openid, 2);
                if(activityRecord==null){
//                    activityRecordService.CouponInsert(2,openid);
                }
                activityRecordMapper.updateEndRecord(1,now,openid);
            }
        } else if(orders.getProductId().equals(2)) {
            LocalDateTime now = LocalDateTime.now();
            activityRecordMapper.updateEndRecord(3,now,openid);
        }
        Users users = userMapper.selectByOpenId(openid);
        if(users.getType()==0){
            userMapper.updataType(openid,1);
        }
        String TransactionType=productInfo.getProductName();
        Date date = new Date();
        Integer Fee=0;
        Long number = orders.getNumber();
        TakeOrders takeOrders = takeOrderMapper.selectById(number);
        String takeOpenid = takeOrders.getOpenid();
        Integer amount = (int) (orders.getAmount()*100);
        //Integer paopaoCoins = (int) (orders.getAmount()*10);
        Integer paopaoCoins = 0;
        UserMoney userMoney = userMoneyMapper.selectById(takeOpenid);
        if(userMoney==null){
            Integer CoinsBefore=0;
            Integer moneyBefore=0;
            transactionsMapper.insert(openid,takeOpenid,orders.getNumber(),CoinsBefore,paopaoCoins,moneyBefore,amount,date,TransactionType,Fee);
            userMoneyMapper.insert(takeOpenid,amount,paopaoCoins);
        }else{
            Integer CoinsAfter = userMoney.getPaopaoCoins() + paopaoCoins;
            Integer  moneyAfter = userMoney.getMoney() + amount;
            transactionsMapper.insert(openid,takeOpenid,orders.getNumber(),userMoney.getPaopaoCoins(),CoinsAfter, userMoney.getMoney(), moneyAfter,date,TransactionType,Fee);
            userMoneyMapper.update(takeOpenid,amount,paopaoCoins);
        }
    }
}
